home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / libs / libelf-0.5 / libelf-0 / libelf-0.5.2 / newscn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-19  |  2.2 KB  |  82 lines

  1. /*
  2. newscn.c - implementation of the elf_newscn(3) function.
  3. Copyright (C) 1995 Michael Riepe <riepe@ifwsn4.ifw.uni-hannover.de>
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #include <private.h>
  21.  
  22. static Elf_Scn*
  23. _buildscn(Elf *elf, size_t size) {
  24.     Elf_Scn *scn;
  25.  
  26.     elf_assert(elf);
  27.     elf_assert(size);
  28.     elf_assert(_elf_scn_init.s_magic == SCN_MAGIC);
  29.     while ((scn = (Elf_Scn*)malloc(sizeof(*scn) + size))) {
  30.     *scn = _elf_scn_init;
  31.     scn->s_elf = elf;
  32.     scn->s_shdr = (char*)(scn + 1);
  33.     memset(scn->s_shdr, 0, size);
  34.     scn->s_scn_flags = ELF_F_DIRTY;
  35.     scn->s_shdr_flags = ELF_F_DIRTY;
  36.     scn->s_freeme = 1;
  37.     if (elf->e_scn_n) {
  38.         scn->s_index = elf->e_scn_n->s_index + 1;
  39.         elf->e_scn_n->s_link = scn;
  40.         elf->e_scn_n = scn;
  41.         return scn;
  42.     }
  43.     elf_assert(scn->s_index == SHN_UNDEF);
  44.     elf->e_scn_1 = elf->e_scn_n = scn;
  45.     }
  46.     seterr(ERROR_MEM_SCN);
  47.     return NULL;
  48. }
  49.  
  50. Elf_Scn*
  51. elf_newscn(Elf *elf) {
  52.     Elf_Scn *scn;
  53.  
  54.     if (!elf) {
  55.     return NULL;
  56.     }
  57.     elf_assert(elf->e_magic == ELF_MAGIC);
  58.     if (!elf->e_ehdr && !elf->e_readable) {
  59.     seterr(ERROR_NOEHDR);
  60.     }
  61.     else if (elf->e_kind != ELF_K_ELF) {
  62.     seterr(ERROR_NOTELF);
  63.     }
  64.     else if (elf->e_ehdr || _elf_cook(elf)) {
  65.     elf_assert(elf->e_ehdr);
  66.     if (elf->e_class == ELFCLASS32) {
  67.         if ((scn = _buildscn(elf, sizeof(Elf32_Shdr)))) {
  68.         ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum = scn->s_index + 1;
  69.         elf->e_ehdr_flags |= ELF_F_DIRTY;
  70.         return scn;
  71.         }
  72.     }
  73.     else if (valid_class(elf->e_class)) {
  74.         seterr(ERROR_UNIMPLEMENTED);
  75.     }
  76.     else {
  77.         seterr(ERROR_UNKNOWN_CLASS);
  78.     }
  79.     }
  80.     return NULL;
  81. }
  82.